<template>
  <el-table :data="list" border fit highlight-current-row style="width: 100%">

    <el-table-column
      align="center"
      element-loading-text="请给我点时间！"
      label="ID"
      v-loading="loading"
      width="65"
    >
      <template slot-scope="scope">
        <span>{{ scope.row.id }}</span>
      </template>
    </el-table-column>

    <el-table-column align="center" label="Date" width="180px">
      <template slot-scope="scope">
        <span>{{ scope.row.timestamp | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
      </template>
    </el-table-column>

    <el-table-column label="Title" min-width="300px">
      <template slot-scope="scope">
        <span>{{ scope.row.title }}</span>
        <el-tag>{{ scope.row.type }}</el-tag>
      </template>
    </el-table-column>

    <el-table-column align="center" label="Author" width="110px">
      <template slot-scope="scope">
        <span>{{ scope.row.author }}</span>
      </template>
    </el-table-column>

    <el-table-column label="Importance" width="120px">
      <template slot-scope="scope">
        <svg-icon :key="n" icon-class="star" v-for="n in +scope.row.importance"/>
      </template>
    </el-table-column>

    <el-table-column align="center" label="Readings" width="95">
      <template slot-scope="scope">
        <span>{{ scope.row.pageviews }}</span>
      </template>
    </el-table-column>

    <el-table-column class-name="status-col" label="Status" width="110">
      <template slot-scope="scope">
        <el-tag :type="scope.row.status | statusFilter">
          {{ scope.row.status }}
        </el-tag>
      </template>
    </el-table-column>

  </el-table>
</template>

<script>
  import {fetchList} from '@/api/article'

  export default {
    filters: {
      statusFilter(status) {
        const statusMap = {
          published: 'success',
          draft: 'info',
          deleted: 'danger'
        };
        return statusMap[status]
      }
    },
    props: {
      type: {
        type: String,
        default: 'CN'
      }
    },
    data() {
      return {
        list: null,
        listQuery: {
          page: 1,
          limit: 5,
          type: this.type,
          sort: '+id'
        },
        loading: false
      }
    },
    created() {
      this.getList()
    },
    methods: {
      getList() {
        this.loading = true;
        this.$emit('create'); // for test
        fetchList(this.listQuery).then(response => {
          this.list = response.data.items;
          this.loading = false;
        })
      }
    }
  }
</script>

